package com.aisile.shop.controller;

import com.aisile.utils.FastDFSClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * // fastDfs的 tracker服务器地址    storage
 * // nginx的访问   http://192.168.25.133:80/ /group1   /m00/
 * @标题 UploadController
 * @作者 Mr.chen
 * @创建日期 2019/1/4 10:48
 * @版本号 1.0
 **/
@RestController
public class UploadController {
    @Value("${NGINX_PATH}")
    private String nginxPath;

    @RequestMapping("upload")
    public Map upload(MultipartFile file) {
        Map map = new HashMap();
        //  error  0    1     url :      message :
        try {
            String oriName = file.getOriginalFilename();
            String suffix = oriName.substring(oriName.lastIndexOf(".")+1);
            FastDFSClient fastDFSClient = new FastDFSClient("classpath:config/client.conf");
            //http://23123// group1   m00/00/00/ asdsadagdfdsfdsfda.jpg
            String imgPath = fastDFSClient.uploadFile(file.getBytes(),suffix);
            map.put("error",0);
            map.put("url",nginxPath+imgPath);
            return map;
        }catch (Exception e){
            map.put("error",1);
            map.put("message","服务器出现异常");
            return map;
        }
    }
}
